<template>
	<view class="main">
		<view class="store">
			<view class="store-name flex-row items-center">
				<image class="store-img" src="https://shops.cg500.com/static/aijiu/dianpu@2x.png" mode=""></image>
				<text class="store-text">{{storeName}}</text>
			</view>
			
			<view class="store-equi flex-row justify-between">
				<text class="equi-text">{{eauimentName}}</text>
				<text class="equi-text">{{eauimentCode}}</text>
			</view>
			
			<view class="set-meal flex-col justify-center">
				<view class="meal-top flex-row items-center">
					<view class="meal-title-line "></view>
					<view class="meal-title">套餐</view>
				</view>
				<view class="meal-list flex-row" v-if="mealList.length > 0">
					<view @click="chooseItem(item,index)" class="meal-list-item flex-col justify-around" :class="currentIndex == index ? 'active' : ''" v-for="(item,index) in mealList" :key="index">
						<view class="meal-list-value">
							<text class="meal-list-money" :class="currentIndex == index ? 'meal-list-money-active' : ''">{{item.money}}</text>
							<text :class="currentIndex == index ? 'meal-list-money-active' : ''">元</text>
						</view>
						<!-- <view class="meal-list-value">
							<text class="" :class="currentIndex == index ? 'meal-list-money-active' : ''">{{item.money}}</text>
							<text :class="currentIndex == index ? 'meal-list-money-active' : ''">元</text><text>/{{item.minutes}}分钟</text>
						</view> -->
						<view class="flex-row justify-center">
							{{item.minutes}}分钟
						</view>
						<view :class="currentIndex == index ? 'meal-list-money-active' : ''" class="meal-list-juan">
							<!-- <text>赠送{{item.juan}}卷</text> -->
							<text>{{item.name}}</text>
						</view>
						<image v-if="currentIndex == index" class="checked-img" src="https://shops.cg500.com/static/aijiu/chenggong@2x.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="cuppon flex-row justify-between">
			<view class="cuppon-lf">
				<text>优惠卷</text>
				<image class="cuppon-img" src="https://shops.cg500.com/static/aijiu/youhuiquan@2x.png" mode=""></image>
			</view>
			<view class="cuppon-rg flex-row items-center">
				<text>暂无优惠卷</text>
				<image class="arrow-rh-img" src="https://shops.cg500.com/static/aijiu/arrow-rg-black.png" mode=""></image>
			</view>
		</view> -->
		
		<view class="cuppon flex-row justify-between">
			<view class="cuppon-lf">
				<text>支付方式</text>
			</view>
			<view class="cuppon-rg flex-row items-center">
				<text>微信支付</text>
				<!-- <image class="arrow-rh-img" src="https://shops.cg500.com/static/aijiu/arrow-rg-black.png" mode=""></image> -->
			</view>
		</view>
		
		<view class="notice flex-row items-center">
			<image class="notice-img" src="https://shops.cg500.com/static/aijiu/dianhua2@2x.png" mode=""></image>
			<text>如有疑问请联系店长</text>
		</view>
		
		<view class="bottom flex-row justify-between">
			<view class="money">￥{{price}}</view>
			<view v-if="isClick" class="button" @click="checkLogin">开启设备</view>
			<view v-else class="button" @click="chongfuClick">开启设备</view>
		</view>
		<!-- 登录弹窗 -->
		<ns-login ref="login" @loginSuccess="loginSuccess"></ns-login>
	</view>
</template>

<script>
	const app = getApp().globalData;
	const {
		$Message
	} = require("../../dist/base/index");
	import nsLogin from "../../components/ns-login/ns-login.vue"
	export default {
		components: {
			nsLogin
		},
		data(){
			return {
				storeName: '',
				eauimentName: '设备1',
				eauimentCode: '',//设备编号
				mealList: [],
				currentIndex: 0,
				shopId: '',
				price: 0,
				mealId: '',//套餐id
				dataPointId: '',//设备id
				isClick: true,//防止点击
			}
		},
		onLoad(options) {
			console.log(options,'options');
			this.shopId = options.shop_id;
			this.eauimentCode = options.cusdeviceNo;
			this.getPackage();
			this.getShopInfo();
		},
		methods: {
			chongfuClick(){
				uni.showToast({
					title: '请勿重复点击',
					icon: 'none'
				})
			},
			loginSuccess(){
				console.log('11111')
				this.userInfo = uni.getStorageSync('userInfo')
				this.user_id = uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').id : ''
				console.log('this.user_id',this.user_id)
				this.login_check = uni.getStorageSync('login_check')
				// this.openEquip()
				// this.getorder(this.user_id);
				// this.photo = uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').photo : ''
				
			},
			// 商家信息
			getShopInfo(){
				var _this = this;
				uni.request({
					url: app.post_url + '/aijiu/api.Shop/shopInfo',
					data:{
						shop_id: this.shopId
					},
					header:{
						'content-type': 'application/json'
					},
					method: 'post',
					success(res) {
						if(res.data.code == 1){
							_this.storeName = res.data.data.name
						}
					}
				})
			},
			// 获取设备变量列表
			getEquiment(id){
				var _this = this;
				uni.request({
					url: app.post_url + '/aijiu/api.Equipment/eqVariable',
					// data: {},
					// dataType: 'json',
					//仅为示例，并非真实的接口地址
					data:{
						cusdevice_no: this.eauimentCode,
						shop_id: this.shopId
					},
					header:{
						'content-type': 'application/json'
					},
					method: 'post',
					success(res) {
						if(res.data.code == 1){
							_this.dataPointId = res.data.data[0].data_point_rel_id
							_this.eauimentCode = res.data.data[0].cusdevice_no
							_this.commandIssuance(id)
						}
					},
					fail(res) {
						uni.showToast({
							title: res.data.info,
							icon: 'none'
						})
					}
				})
			},
			// 命令下发
			commandIssuance(orderId){
				var _this = this;
				uni.request({
					url: app.post_url + '/aijiu/api.Equipment/eqCommand',
					data:{
						cusdeviceNo: this.eauimentCode,
						dataPointId: this.dataPointId,
						value: '1',
						shop_id: this.shopId,
						order_id: orderId
					},
					header:{
						'content-type': 'application/json'
					},
					method: 'post',
					success(res) {
						if(res.data.code == 1){
							// uni.reLaunch({
							// 	url: '/pages/shop/equipmentStatus/index?cusdeviceNo=' + _this.eauimentCode + '&orderId=' + orderId
							// })
							
							uni.reLaunch({
								url: '/pages/shop/equipmentStatus/index?orderId=' + orderId + '&enterType=1'
							})
						}
					},
					fail(res) {
						uni.showToast({
							title: res.data.info,
							icon: 'none'
						})
						
					}
				})
			},
			checkLogin(){
				if(!uni.getStorageSync("userId")){
					this.$refs.login.open()
					return;
				}
				this.openEquip()
				
			},
			// 创建订单
			openEquip(){
				// if(!uni.getStorageSync("userId")){
				// 	this.$refs.login.open()
				// 	return;
				// }
				var _this = this;
				this.isClick = false;
				uni.request({
					url: app.post_url + '/aijiu/api.Order/createOrder',
					// data: {},
					// dataType: 'json',
					//仅为示例，并非真实的接口地址
					data:{
						uid: uni.getStorageSync("userId"),
						meal_id: this.mealId,
						order_amount: this.price,
						cusdevice_no: this.eauimentCode,
						shop_id: this.shopId
					},
					header:{
						'content-type': 'application/json'
					},
					method: 'post',
					success(res) {
						if(res.data.code == 1){
							let order_id = res.data.data[1]
							
							uni.requestPayment({
							  timeStamp: res.data.data[0].params.timeStamp,
							  nonceStr: res.data.data[0].params.nonceStr,
							  package: res.data.data[0].params.package,
							  signType: 'MD5',
							  paySign: res.data.data[0].params.paySign,
							
							  success(res) {
								  _this.getEquiment(order_id)
								  
							  },
							
							  fail(res) {
								  uni.showToast({
								  	title: '支付失败',
								  	icon: 'error'
								  })
								  _this.isClick = true;
								  
							  }
							
							});
						}else {
							uni.showToast({
								title: res.data.info,
								icon: 'none'
							})
						}
					},
					fail(res) {
						_this.isClick = true;
						uni.showToast({
							title: res.data.info,
							icon: 'none'
						})
					}
							
				});
			},
			// 订单支付
			orderPay(orderId){
				console.log(orderId,'orderId')
				uni.request({
					url: app.post_url + '/aijiu/api.Order/payment',
					// data: {},
					// dataType: 'json',
					//仅为示例，并非真实的接口地址
					data:{
						order_id: orderId
					},
					header:{
						'content-type': 'application/json'
					},
					method: 'post',
					success(res) {
						if(res.data.code == 1){
							
						}
					},
					fail(res) {
						
					}
							
				});
			},
			chooseItem(item,index){
				this.currentIndex = index
				this.price = this.mealList[index].money
				this.mealId = this.mealList[index].id
			},
			//获取套餐列表
			getPackage: function() {
				var _this = this;
			
				uni.request({
					url: app.post_url + '/aijiu/api.Meal/mealList',
					// data: {},
					// dataType: 'json',
					//仅为示例，并非真实的接口地址
					data:{
						shop_id: this.shopId
					},
					header:{
						'content-type': 'application/json'
					},
					method: 'post',
					success(res) {
						if(res.data.code == 1){
							_this.mealList = res.data.data
							_this.price = _this.mealList[0].money
							_this.mealId = _this.mealList[0].id
						}
						// _this.background = res.data.data.list
						// _this.num = res.data.data.num
						// _this.price = res.data.data.price
					},
					fail(res) {
						console.log(res,'fail')
					}
			
				});
			},
		}
	}
</script>

<style>
	@import "@/common/css/common.css";
	@import "./index.css";
</style>